package com.study.zzedu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.Chapter;
import com.study.zzedu.entity.CourseItem;
import com.study.zzedu.widget.ProgressBarView;

import java.util.List;

/**
 * Created by kiros on 2020/9/5.
 * 课程Adapter
 */

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.ViewHolder> {

    private Context context;
    private List<CourseItem> courseItems;
    private OnItemClickListener listener;

    public interface OnItemClickListener{
        void onItemClick(Chapter beanItem);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public CourseAdapter(Context context, List<CourseItem> courseItems) {
        this.context = context;
        this.courseItems = courseItems;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_course_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(courseItems.size()<1)
            return;
        if (position == 0) {
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.ll_course_item.getLayoutParams();
            layoutParams.setMargins(0, 20, 0, 0);
            holder.ll_course_item.setLayoutParams(layoutParams);
        }

        holder.courseName.setText(courseItems.get(position).getChapterTitle());
        holder.l_course.removeAllViews();
        final List<Chapter> chapters = courseItems.get(position).getChapters();

        if (courseItems.size() > 0) {
            for (int i = 0; i < chapters.size(); i++) {
                View course_item_view = LayoutInflater.from(context).inflate(R.layout.course_item, null);
                LinearLayout course_item = course_item_view.findViewById(R.id.course_item);
                TextView tv_course = course_item_view.findViewById(R.id.tv_course);
                ImageView iv_download = course_item_view.findViewById(R.id.iv_download);
                tv_course.setText(chapters.get(i).getChapterName());
                holder.l_course.addView(course_item_view);

                final int finalI = i;
                course_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.onItemClick(chapters.get(finalI));
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return courseItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView courseName;
        private LinearLayout ll_course_item, l_course;

        public ViewHolder(View itemView) {
            super(itemView);
            ll_course_item = itemView.findViewById(R.id.ll_course_item);
            courseName = itemView.findViewById(R.id.course_name);
            l_course = itemView.findViewById(R.id.l_course);
        }
    }
}
